package com.wen.service.impl;

import com.github.pagehelper.PageHelper;
import com.wen.dao.UserMapper;
import com.wen.pojo.Role;
import com.wen.pojo.Users;
import com.wen.service.UserService;
import com.wen.utils.BCryptPasswordEncoderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Users byUserName = null;
        try {
            byUserName = userMapper.findByUserName(username);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("=>" + byUserName);
        /**
         * 出来自己的用户对象封装成UserDetails
         */
        // User user = new User(byUserName.getUserName(), "{noop}" + byUserName.getPassWord(), getAuthority(byUserName.getRoles()));
        User user = new User(byUserName.getUserName(), byUserName.getPassWord(), byUserName.getStatus() == 0 ? false : true, true, true, true, getAuthority(byUserName.getRoles()));
        return user;
    }

    public List<SimpleGrantedAuthority> getAuthority(List<Role> roles) {
        List<SimpleGrantedAuthority> list = new ArrayList<>();
        for (Role role : roles
        ) {
            list.add(new SimpleGrantedAuthority("ROLE_" + role.getRoleName()));

        }
        return list;
    }

    @Override
    public List<Users> findAll(int page, int size) throws Exception {
        PageHelper.startPage(page, size);
        return userMapper.findAll();
    }

    @Override
    public int add(Users users) throws Exception {
        users.setPassWord(BCryptPasswordEncoderUtils.encodePassword(users.getPassWord()));
        return userMapper.add(users);
    }

    @Override
    public Users findById(int id) throws Exception {
        return userMapper.findById(id);
    }

    @Override
    public int deleteById(int id) {
        return userMapper.deleteById(id);
    }

    @Override
    public int updateById(Users users) throws Exception {
        users.setPassWord(BCryptPasswordEncoderUtils.encodePassword(users.getPassWord()));
        return userMapper.updateById(users);
    }

    @Override
    public List<Role> findOtherRoles(int userId) throws Exception {
        return userMapper.findOtherRoles(userId);
    }

    @Override
    public void addRoleToUser(int userId, int[] roleIds) {
        for (int roleId : roleIds) {
            userMapper.addRoleToUser(userId,roleId);
        }
    }
}
